/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.zjdt.epserver.listener;

import com.zjdt.epserver.db.dao4mysql.MisceDaoImpl;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import org.dusin.mobile.util.MobileUserList;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 * Web application lifecycle listener.
 *
 * @author tengyu
 */
@WebListener()
public class ContextListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent sce) {     
        ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext());
        MisceDaoImpl instance = (MisceDaoImpl) context.getBean("misceDao");
        String recommendCode = instance.getConstant("recommendCode");
        sce.getServletContext().setAttribute("recommendCode",recommendCode); 
        MobileUserList siters = new MobileUserList("siters");  //驻厂员
        siters.startCheckTimeout();
        
        MobileUserList drivers = new MobileUserList("drivers");  //司机
        drivers.startCheckTimeout();
        
        sce.getServletContext().setAttribute("siters",siters); //   
        sce.getServletContext().setAttribute("drivers",drivers); 
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        MobileUserList siters = (MobileUserList)sce.getServletContext().getAttribute("siters");
        if(siters!=null)
            siters.stopCheckTimeout();        
        MobileUserList drivers = (MobileUserList)sce.getServletContext().getAttribute("drivers");
        if(drivers!=null)
            drivers.stopCheckTimeout();        
    }
}
